//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "client\fn_clientSetupLead.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_object","_action","_parameters","_destination_msg","_die_msg","_shared_destination","_shared_alive","_destination_code","_die_code","_destination","_evt_idx","_is_arrived","_distance","_action_display_timer"];
DEFAULT_PARAM(_object,TMF_REGISTER_OBJECT,objNull);
DEFAULT_PARAM(_action,TMF_REGISTER_ACTION,[]);
DEFAULT_PARAM(_instance_id,TMF_REGISTER_INSTANCE,TMF_SERVER_INSTANCE_ID);

if (!isnull _object) then {
	_action_id = [_action] call FFUNC(actionGetID);
	TRACE_1("Action",_action);
	_parameters = [_action] call FFUNC(actionGetParams);

	if (_object isKindOf "Man") then {
		_destination_msg = [_parameters, "DEST_MSG", "",THIS_FILE] call FFUNC(hashGet);
		_die_msg = [_parameters, "DIE_MSG", "",THIS_FILE] call FFUNC(hashGet);
		_shared_destination = [_parameters, "SHARED_DEST_VAR", "",THIS_FILE] call FFUNC(hashGet);
		_shared_alive = [_parameters, "SHARED_ALIVE_VAR", "",THIS_FILE] call FFUNC(hashGet);
		_destination = [_parameters, "DESTINATION", [],THIS_FILE] call FFUNC(hashGet);
		_destination_code = [_parameters, "DEST_CODE", "",THIS_FILE] call FFUNC(hashGet);
		_die_code = [_parameters, "DIE_CODE", "",THIS_FILE] call FFUNC(hashGet);
		_shared_destination = _shared_destination call FFUNC(getStringVar);
		_shared_alive = _shared_alive call FFUNC(getStringVar);

		_dest_center = GETVAL(_destination,0,[]);
		_dest_size = GETVAL(_destination,1,10);
		if (typename _dest_center == typename "") then { _dest_center = getMarkerPos _dest_center; };
		if (typename _dest_size == typename []) then {
			_distance = (_dest_size select 0) max (_dest_size select 1);
		} else {
			_distance = _dest_size;
		};
		
		_obj_var_arrived = format["mission_person_arrived_%1",_action_id];
		_is_arrived = OGETVAR(_object,_obj_var_arrived,false);
		If (isNil "_is_arrived") then {
			_is_arrived = false;
		} else {
			if (_distance < (_object distance _dest_center)) then {
				_is_arrived = false;
			};
		};
		
		TRACE_2("Object Arrived",_is_arrived,(ARR_2(player,"Rescue") call FFUNC(playerHasRole)));
		If ((!_is_arrived) && ([player,"Rescue"] call FFUNC(playerHasRole))) then {
			_act_id = [_object,_action,"LEAD_ACTION",-1] call FFUNC(actionGetLocal);
			if (_act_id >= 0) then {
				TRACE_1("Removed Action",_act_id);
				_object removeAction _act_id;
			};
			_action_title = ["STR_TMF_LEAD_DEFACTION"] call FFUNC(translate);
			_arguments = [];
			_arguments set [TMF_EXECUTE_ACTION_PARAM_ACTID, _action_id];
			_action_display_timer = QGVAR(action_display_timer);
			_object setVariable [_action_display_timer,0];
			_condition = format["((!isplayer(leader _target)) && {vehicle player == player} && {(alive _target)} && {(!(_target getVariable '%1'))}) && {(_target getVariable '%2' < time)}",_obj_var_arrived,_action_display_timer];
			_act_id = _object addAction [_action_title, QPATH(addAction.sqf),_arguments,10,True,True,"",_condition];
			TRACE_2("Added Action",_act_id,_condition);
			[_object,_action,"LEAD_ACTION",_act_id] call FFUNC(actionSetLocal);

			_evt_idx = [_object,_action,"LEAD_KILLED_EVENT",-1] call FFUNC(actionGetLocal);
			if (_evt_idx < 0) then {
				_evt_idx = _object addEventHandler ["killed", Format["[_this, %1] call %2;",_action_id,QUOTE(FFUNC(eventObjectiveLeadFailed))]];
				[_object,_action,"LEAD_KILLED_EVENT",_evt_idx] call FFUNC(actionSetLocal);
			};
		};
	} else {
		ERROR_1("Object not a man", typeof _object);
	};
} else {
	ERROR("Object not set");
};
TRACE("End");
TMFPROFILERSTOP;